package com.maiworld.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maiworld.content.service.AnswerFeedbackService;
import com.maiworld.content.service.ArticleService;
import com.maiworld.content.service.SubjectLC2Service;
import com.maiworld.content.service.SubjectLService;
import com.maiworld.pojo.AnswerFeedback;
import com.maiworld.pojo.News;
import com.maiworld.pojo.Subject;

@RestController
@RequestMapping("/v1/test")
public class ExaminationTestController {
	
	@Reference
	private SubjectLService subjectLService;
	
	@Reference
	private SubjectLC2Service subjectLC2Service;
	
	@Reference
	private ArticleService articleService;
	
	@Reference
	private AnswerFeedbackService answerFeedbackService;
	
	@RequestMapping(value = "/sub1/main",method =RequestMethod.GET)
	public Map<String,Object> testMain(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取考友小视频
			List<News> list = new ArrayList<News>();
			list = articleService.selectByClassIdLimit(Long.parseLong("7"), status, Integer.parseInt("0"), Integer.parseInt("5"));
			
			Map<String,Integer> testNum = subjectLService.countSub1Number();
			result.put("testNum", testNum);
			
			if(list.isEmpty()) {
				result.put("videolist",null);
			}else {
				result.put("videolist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 获取模拟题  科目一
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page 第几套
	 * @return
	 */
	@RequestMapping(value = "/sub1/simulation",method =RequestMethod.GET)
	public Map<String,Object> simulation(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLService.orderTest(Integer.parseInt(page));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 获取一套题    随机题目  
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @return
	 */
	@RequestMapping(value = "/sub1/rand",method =RequestMethod.GET)
	public Map<String,Object> randsub1(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLService.randTest();
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 获取判断题 科目一
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sub1/judge",method =RequestMethod.GET)
	public Map<String,Object> judgesub1(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page,
			@RequestParam("pageSize") String pageSize
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLService.orderJudge(Integer.parseInt(page), Integer.parseInt(pageSize));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 获取单选题 科目一
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sub1/single",method =RequestMethod.GET)
	public Map<String,Object> singlesub1(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page,
			@RequestParam("pageSize") String pageSize
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLService.orderSingle(Integer.parseInt(page), Integer.parseInt(pageSize));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 交卷
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/sub1/paper",method =RequestMethod.POST)
	public Map<String,Object> handPaper(
			@RequestBody String params
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(params);
			//数据处理
			JSONObject jsonobjectF = JSONObject.parseObject(params);
			JSONObject jsonobject = jsonobjectF.getJSONObject("params");
			
			JSONObject resultques = jsonobject.getJSONObject("result");
			
			JSONObject userinfo = jsonobject.getJSONObject("userinfo");
			
			JSONArray questions = jsonobject.getJSONArray("questions");
			
//			System.out.println(resultques.toString());
//			System.out.println(userinfo.toString());
//			System.out.println(questions.toString());
			AnswerFeedback answerFeeb = new AnswerFeedback();
			answerFeeb.setSid(Long.parseLong(userinfo.getString("sid")));
			answerFeeb.setStatus(userinfo.getString("status"));
			answerFeeb.setScore(resultques.getString("score"));
			answerFeeb.setCorrect(resultques.getString("correct"));
			answerFeeb.setIncorrect(resultques.getString("error"));
			answerFeeb.setSubject("1");
			answerFeeb.setType("1");
			Long feed_id = answerFeedbackService.addAnswerFeedback(answerFeeb);
			
			
			List<Subject> correct = new ArrayList<Subject>();
			List<Subject> error = new ArrayList<Subject>();
			
			for(int i=0;i < questions.size();i++) {
				JSONObject question = questions.getJSONObject(i);
				if(Integer.parseInt(question.getString("myanswer"))!= 0) {
					Subject sub = new Subject();
					sub.setFeedId(feed_id);
					sub.setQid(Long.parseLong(question.getString("id")));
					sub.setQuestion(question.getString("question"));
					sub.setAnswer(question.getString("answer"));
					sub.setStudentAnswer(question.getString("myanswer"));
					sub.setItem1(question.getString("item1"));
					sub.setItem2(question.getString("item2"));
					sub.setItem3(question.getString("item3"));
					sub.setItem4(question.getString("item4"));
					sub.setUrl(question.getString("url"));
					sub.setTopicTypes(question.getString("topicTypes"));
					
					sub.setExplains(question.getString("explains"));
					
					
					if(Integer.parseInt(question.getString("myanswer")) == Integer.parseInt(question.getString("answer"))) {
						//存到正确表里面
						correct.add(sub);
					}else {
						//错误表里面
						error.add(sub);
					}
				}
			}
			for(Subject subss:error) {
				System.out.println(subss);
			}
			answerFeedbackService.insertCorrect(correct);
			answerFeedbackService.insertError(error);
			
			AnswerFeedback answerFeedback = new AnswerFeedback();
			answerFeedback.setSid(Long.parseLong(userinfo.getString("sid")));
			answerFeedback.setSubject(userinfo.getString("subject"));
			List<AnswerFeedback> list = new ArrayList<AnswerFeedback>();
			list = answerFeedbackService.selectAnswerFeedback(answerFeedback);
			int flag = 0;
			for(AnswerFeedback af:list) {
				if(Integer.parseInt(af.getScore()) > 90) {
					flag++;
//					System.out.println(flag);
				}else {
					if(flag < 3) {
						flag = 0;						
					}
				}
			}
			result.put("pass", 0);
			if(flag >=3) {
				int res = answerFeedbackService.addAnswerFeedbackL(Long.parseLong(userinfo.getString("sid")), userinfo.getString("subject"), userinfo.getString("status"));
				if(res != 0) {
					result.put("pass", 1);
				}
			}
			
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 查询有多少套错题信息  
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param sid
	 * @return
	 */
	@RequestMapping(value = "/sub/errorPaper",method =RequestMethod.GET)
	public Map<String,Object> errorPaper(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("sid") String sid
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题结果
			AnswerFeedback answerFeedback = new AnswerFeedback();
			answerFeedback.setSid(Long.parseLong(sid));
			answerFeedback.setSubject(subject);
			List<AnswerFeedback> list = new ArrayList<AnswerFeedback>();
			list = answerFeedbackService.selectAnswerFeedback(answerFeedback);
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 查询错误试卷内容题目
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param feedid
	 * @return
	 */
	@RequestMapping(value = "/sub/errorQues",method =RequestMethod.GET)
	public Map<String,Object> errorQues(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("feedid") String feedid
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			Subject sub = new Subject();
			sub.setFeedId(Long.parseLong(feedid));
			List<Subject> list = new ArrayList<Subject>();
			list = answerFeedbackService.selectQuestion(sub);
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	
	
	
	/**
	 * 科目四 首页
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @return
	 */
	@RequestMapping(value = "/sub4/main",method =RequestMethod.GET)
	public Map<String,Object> testMainSub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取考友小视频
			List<News> list = new ArrayList<News>();
			list = articleService.selectByClassIdLimit(Long.parseLong("20"), status, Integer.parseInt("0"), Integer.parseInt("5"));
			
			Map<String,Integer> testNum = subjectLC2Service.countSub4NumberC2();
			result.put("testNum", testNum);
			
			if(list.isEmpty()) {
				result.put("videolist",null);
			}else {
				result.put("videolist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 科目四获取 模拟题 50道   顺序获取
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/sub4/simulation",method =RequestMethod.GET)
	public Map<String,Object> simulationSub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLC2Service.orderTestC2(Integer.parseInt(page));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 获取随机题目      
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @return
	 */
	@RequestMapping(value = "/sub4/rand",method =RequestMethod.GET)
	public Map<String,Object> randsub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLC2Service.randTestC2();
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 获取判断题 科目四
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sub4/judge",method =RequestMethod.GET)
	public Map<String,Object> judgesub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page,
			@RequestParam("pageSize") String pageSize
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLC2Service.orderJudgeC2(Integer.parseInt(page), Integer.parseInt(pageSize));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 获取单选题 科目四
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sub4/single",method =RequestMethod.GET)
	public Map<String,Object> singlesub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page,
			@RequestParam("pageSize") String pageSize
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLC2Service.orderSingleC2(Integer.parseInt(page), Integer.parseInt(pageSize));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	/**
	 * 多选题 科目四
	 * @param status
	 * @param openid
	 * @param sessionkey
	 * @param subject
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sub4/multiple",method =RequestMethod.GET)
	public Map<String,Object> multiplesub4(
			@RequestParam("status") String status,
			@RequestParam("openid") String openid,
			@RequestParam("session_key") String sessionkey,
			@RequestParam("subject") String subject,
			@RequestParam("page") String page,
			@RequestParam("pageSize") String pageSize
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(status);
			//获取模拟题
			List<Subject> list = new ArrayList<Subject>();
			list = subjectLC2Service.orderMultipleC2(Integer.parseInt(page), Integer.parseInt(pageSize));
			
			if(list.isEmpty()) {
				result.put("datalist",null);
			}else {
				result.put("datalist",list);
			}
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	
	/**
	 * 交卷  科目四
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/sub4/paper",method =RequestMethod.POST)
	public Map<String,Object> handPaperSub4(
			@RequestBody String params
		) {
			Map<String,Object> result = new HashMap<String,Object>();
			System.out.println(params);
			//数据处理
			JSONObject jsonobjectF = JSONObject.parseObject(params);
			JSONObject jsonobject = jsonobjectF.getJSONObject("params");
			
			JSONObject resultques = jsonobject.getJSONObject("result");
			
			JSONObject userinfo = jsonobject.getJSONObject("userinfo");
			
			JSONArray questions = jsonobject.getJSONArray("questions");
			
//			System.out.println(resultques.toString());
//			System.out.println(userinfo.toString());
//			System.out.println(questions.toString());
			AnswerFeedback answerFeeb = new AnswerFeedback();
			answerFeeb.setSid(Long.parseLong(userinfo.getString("sid")));
			answerFeeb.setStatus(userinfo.getString("status"));
			answerFeeb.setScore(resultques.getString("score"));
			answerFeeb.setCorrect(resultques.getString("correct"));
			answerFeeb.setIncorrect(resultques.getString("error"));
			answerFeeb.setSubject("4");
			answerFeeb.setType("1");
			Long feed_id = answerFeedbackService.addAnswerFeedback(answerFeeb);
			
			
			List<Subject> correct = new ArrayList<Subject>();
			List<Subject> error = new ArrayList<Subject>();
			Subject sub = new Subject();
			for(int i=0;i < questions.size();i++) {
				JSONObject question = questions.getJSONObject(i);
				if(Integer.parseInt(question.getString("myanswer"))!= 0) {
					sub.setFeedId(feed_id);
					sub.setQid(Long.parseLong(question.getString("id")));
					sub.setQuestion(question.getString("question"));
					sub.setAnswer(question.getString("answer"));
					sub.setStudentAnswer(question.getString("myanswer"));
					sub.setItem1(question.getString("item1"));
					sub.setItem2(question.getString("item2"));
					sub.setItem3(question.getString("item3"));
					sub.setItem4(question.getString("item4"));
					sub.setUrl(question.getString("url"));
					sub.setTopicTypes(question.getString("topicTypes"));
					
					sub.setExplains(question.getString("explains"));
					
					if(Integer.parseInt(question.getString("myanswer")) == Integer.parseInt(question.getString("answer"))) {
						//存到正确表里面
						correct.add(sub);
					}else {
						//错误表里面
						error.add(sub);
					}
				}
			}
//			for(Subject subss:error) {
//				System.out.println(subss);
//			}
			answerFeedbackService.insertCorrect(correct);
			answerFeedbackService.insertError(error);
			
			AnswerFeedback answerFeedback = new AnswerFeedback();
			answerFeedback.setSid(Long.parseLong(userinfo.getString("sid")));
			answerFeedback.setSubject(userinfo.getString("subject"));
			List<AnswerFeedback> list = new ArrayList<AnswerFeedback>();
			list = answerFeedbackService.selectAnswerFeedback(answerFeedback);
			int flag = 0;
			for(AnswerFeedback af:list) {
				if(Integer.parseInt(af.getScore()) > 90) {
					flag++;
//					System.out.println(flag);
				}else {
					if(flag < 3) {
						flag = 0;						
					}
				}
			}
			result.put("pass", 0);
			if(flag ==3) {
				int res = answerFeedbackService.addAnswerFeedbackL(Long.parseLong(userinfo.getString("sid")), userinfo.getString("subject"), userinfo.getString("status"));
				if(res != 0) {
					result.put("pass", 1);
				}
			}
			
			result.put("statusCode", 200);
			result.put("message", "成功");
			return result;
	}
	

	
}
